import wpath
from scripts.deploy.accounts import *

VAULTS = [yVault, yWETH, yDelegatedVault]


def deploy():
    nonce_list = [5, 6, 7]

    N = len(VAULTS)

    # 部署金库合约
    def vault_deployment(gov, token, controller, index):

        Vault = VAULTS[index]
        nonce = nonce_list[index]

        # 指定nonce,可以保证合约地址一样
        vault = gov.deploy(Vault, token, controller, nonce=nonce)

        assert vault.governance() == gov
        assert vault.controller() == controller
        assert vault.token() == token

        assert vault.name() == "yearn " + token.name()
        assert vault.symbol() == "y" + token.symbol()
        assert vault.decimals() == token.decimals()

    for index in range(N):
        vault_deployment(gov, token, controller, index)


def config():
    contract_address_list = [
        "0x3c902CDA1398c055C46208746A2eEeC7917eD360",  # yVault
        "0x46af1E31ec1efC6E73B724014dda489A0612F4bd",  # yWETH
        "0xA55d16e8Abd2182052baD61C2bD5d1B22725027e",  # yDelegatedVault
        "0x79435EBAcDF1A16AE6dB45839521A29cE6647B03",  # StrategyCreamYFI
    ]

    # 金库合约设置参数
    def vault_setParams(accounts, gov, token, controller, getter, setter, val, index):
        if not val:
            val = accounts[1]

        # 加载已经部署的合约
        contract_address = contract_address_list[index]
        vault = VAULTS[index].at(contract_address)

        if not hasattr(vault, getter):
            return

        getattr(vault, setter)(val, {"from": accounts[1]})
        getattr(vault, setter)(val, {"from": gov})
        assert getattr(vault, getter)() == val

        if getter == "governance":
            getattr(vault, setter)(val, {"from": gov})

    params = [
        ("min", "setMin", 9000),
        ("healthFactor", "setHealthFactor", 100),
        ("controller", "setController", None),
        ("governance", "setGovernance", None),
    ]

    for index in range(N):
        for getter, setter, val in params:
            vault_setParams(
                accounts, gov, token, controller, getter, setter, val, index
            )
